
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rt.schedulebase.dto.DictCommonUseDto;
import com.rt.schedulebase.entity.DictCommonUse;
import com.rt.schedulebase.entity.commonentity.PageResultUtil;
import com.rt.schedulebase.mapper.DictCommonUseMapper;
import com.rt.schedulenew.api.IDictCommonUseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.global.ErrorCode;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DictCommonUseServiceImpl extends ServiceImpl<DictCommonUseMapper, DictCommonUse> implements IDictCommonUseService {

    @Override
    public List<DictCommonUse> getList(DictCommonUseDto dto) {
        Map<String, Object> map = new HashMap<>();
        map.put("VALUE", dto.getValue());
        QueryWrapper<DictCommonUse> qw = new QueryWrapper<>();
        qw.allEq(map, false);

        if (StringUtils.isNotBlank(dto.getExamClass())) {
            qw.in("EXAM_CLASS", CommonUtil.split2List(dto.getExamClass()));
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            qw.eq("NAME", dto.getName());
        } else {
            qw.in("NAME", "REMINDER_SIGN", "REMINDER_SCHEDULE");
        }

        if (StringUtils.isNotBlank(dto.getReserve1())) {
            qw.in("RESERVE1", CommonUtil.split2List(dto.getReserve1()));
        }
        qw.orderByAsc("NAME", "SORT_NO");
        List<DictCommonUse> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<DictCommonUse> getListAll() {
        return baseMapper.selectList(null);
    }


    @Override
    public DictCommonUse getInfo(DictCommonUseDto dto) {
        QueryWrapper<DictCommonUse> qw = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("NAME", dto.getOldName());
        map.put("EXAM_CLASS", dto.getOldExamClass());
        map.put("RESERVE1", dto.getOldReserve1());
        qw.allEq(map, false);
        return baseMapper.selectOne(qw);
    }

    @Override
    public JsonResult insertCommonUse(DictCommonUse dictCommonUse) {
        QueryWrapper<DictCommonUse> qw = new QueryWrapper<>();
        ((qw.eq("EXAM_CLASS", dictCommonUse.getExamClass()))
                .eq("NAME", dictCommonUse.getName()))
                .eq("RESERVE1", dictCommonUse.getReserve1());
        int count = baseMapper.selectCount(qw);
        if (count > 0) {
            return JsonResultUtil.failure("该记录已存在!");
        }
        boolean bool = retBool(baseMapper.insert(dictCommonUse));
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }

    /**
     * 通用字典分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public JsonResult getListPage(DictCommonUseDto dto) {
        Page<DictCommonUse> page = new Page<>();
        if (dto.getPageNo() != null) {
            page = PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        }
        List<DictCommonUse> list = getList(dto);
        JsonResult jsonResult = PageResultUtil.success(ErrorCode.SUCCESS.getValue(), list, page);
        return jsonResult;
    }

    @Override
    public boolean updateByDto(DictCommonUseDto dto) {
        // 长度验证
        if (StringUtils.isNotBlank(dto.getValue()) && dto.getValue().length() > 4000) {
            return false;
        }
        UpdateWrapper<DictCommonUse> uw = new UpdateWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("NAME", dto.getOldName());
        map.put("EXAM_CLASS", dto.getOldExamClass());
        map.put("RESERVE1", dto.getOldReserve1());
        uw.allEq(map);
        return retBool(baseMapper.update(dto, uw));
    }

    @Override
    public boolean deleteByDto(DictCommonUse dictCommonUse) {
        // 删除时参数验证
        if (StringUtils.isEmpty(dictCommonUse.getExamClass())
                || StringUtils.isEmpty(dictCommonUse.getName())
                || StringUtils.isEmpty(dictCommonUse.getReserve1())) {
            return false;
        }
        UpdateWrapper<DictCommonUse> uw = new UpdateWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("EXAM_CLASS", dictCommonUse.getExamClass());
        map.put("NAME", dictCommonUse.getName());
        map.put("VALUE", dictCommonUse.getValue());
        map.put("RESERVE1", dictCommonUse.getReserve1());
        uw.allEq(map);
        return retBool(baseMapper.delete(uw));
    }
}
